// leaderPerson/pages/center/center.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        page_sign: "my",
        alert: 0,
        tab: [ {
            title: "今日",
            id: "1"
        }, {
            title: "本团",
            id: "2"
        }, {
            title: "本周",
            id: "3"
        }, {
            title: "本月",
            id: "4"
        }, {
            title: "全部",
            id: "5"
        } ],
        chooseId: 1,
        leader_receive: 0
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        var isIPX = app.globalData.isIPX;
        this.setData({
            isIPX: isIPX,
            domain: app.globalData.domain.slice(0, -10)
        });
        var that = this;
        that.setData({
            clerk: wx.getStorageSync("clerk")
        });
        this.getBase();
        //基础信息
                this.getData();
        //个人信息
                this.getLeaderEntry();
        //是否休市
                this.leaderSale();
        //团长中心数据
                this.getMyLeaderInfo();
        //获取团长信息
        },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
        this.getBase();
        //基础信息
                this.getData();
        //个人信息
                this.getLeaderEntry();
        //是否休市
                this.leaderSale();
        //团长中心数据
                this.getMyLeaderInfo();
        //获取团长信息
        },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        var that = this;
        var alert = that.data.alert;
        var leader_id = that.data.leader_id;
        var code = wx.getStorageSync("code");
        console.log(leader_id);
        var leader_share = that.data.leader_share;
        var user_share = that.data.user_share;
        if (alert == 1) {
            return {
                title: that.data.apptitle,
                imageUrl: leader_share,
                path: "/pages/group_info/group_info?my_leaderID=" + leader_id
            };
        } else {
            return {
                title: that.data.apptitle,
                imageUrl: user_share,
                path: "/pages/index/index?my_leaderID=" + leader_id + "&code=" + code
            };
        }
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: res.extra.leader_title ? res.extra.leader_title : "团长" + "入口"
                });
                that.setData({
                    color_main: res.extra.color_main,
                    time_choose: res.extra.time_choose,
                    apptitle: res.extra.title,
                    leader_share: res.extra.leader_share,
                    user_share: res.extra.user_share,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    if_leadersales_show: res.extra.if_leadersales_show,
                    leader_detail_edit: res.extra.leader_detail_edit,
                    phone: res.extra.phone,
                    sleep_on: res.extra.sleep_on,
                    face_pay_set: res.extra.face_pay_set,
                    leader_receive: res.extra.leader_receive
                });
            }
        });
    },
    getData: function() {
        var that = this;
        var clerk = that.data.clerk;
        console.log("wom_status" + clerk.wom_status);
        console.log("leader_openid" + clerk.leader_openid);
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        // 基本信息获取
                common.request({
            endpoint: "Leader/leaderpersonal/leader_personal",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                //团长等级
                                var level_name = res.extra.level_name;
                if (level_name) {
                    if (level_name.length > 5) {
                        level_name = level_name.substring(0, 5) + "...";
                    } else {
                        level_name = level_name;
                    }
                }
                that.setData({
                    info: res.extra,
                    leader_id: res.extra.leader_id,
                    level_name: level_name
                });
            }
        });
        // 旗下会员获取
                common.request({
            endpoint: "Leader/leaderpersonal/leader_children",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                that.setData({
                    member_num: res.extra
                });
            }
        });
        //旗下团长获取
                common.request({
            endpoint: "Leader/leaderpersonal/leader_recommend",
            method: "post",
            data: {
                openid: openid
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                that.setData({
                    group_num: res.extra
                });
            }
        });
    },
    /**
   * 获取团长信息
   */
    getMyLeaderInfo: function(leader_id_share) {
        var that = this;
        common.request({
            endpoint: "Leader/leaderpersonal/leader_info",
            method: "post",
            data: {
                openid: wx.getStorageSync("user").openid
            },
            success: res => {
                that.setData({
                    myleaderinfo: res.extra
                });
                that.getIndexPoster();
            },
            fail(err) {
                let alertMsg = "提示";
                wx.showModal({
                    title: alertMsg,
                    content: err.message,
                    showCancel: false
                });
            }
        });
    },
    getLeaderEntry: function() {
        let that = this;
        let openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Leader/checkstatus/leaderentry",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                console.log(res);
                if (res.extra.reward.length) {
                    that.setData({
                        recommend: 1,
                        reward: res.extra.reward
                    });
                }
                that.setData({
                    leader_rest: res.extra.leader_rest
                });
            }
        });
    },
    chooseDay: function(e) {
        this.setData({
            chooseId: e.currentTarget.dataset.id
        });
        this.leaderSale();
    },
    leaderSale: function() {
        let _this = this;
        common.request({
            endpoint: "leader/leader-sale",
            method: "post",
            data: {
                day: _this.data.chooseId
            },
            success: res => {
                _this.setData({
                    saleData: res.extra
                });
            }
        });
    },
    onPageTap: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    },
    alert: function() {
        var alert = this.data.alert;
        if (alert == 0) {
            alert = 1;
        } else {
            alert = 0;
        }
        this.setData({
            alert: alert
        });
    },
    makeposter: function() {
        wx.navigateTo({
            url: "/pages/share/share?types=group"
        });
    },
    makeposter2: function() {
        let that = this;
        var goUrl = "/poster/pages/indexShare/indexShare?myleaderinfo=" + encodeURIComponent(JSON.stringify(that.data.myleaderinfo));
        wx.navigateTo({
            url: goUrl
        });
    },
    alert2: function() {
        var alert2 = this.data.alert2;
        if (alert2 == 0) {
            alert2 = 1;
        } else {
            alert2 = 0;
        }
        this.setData({
            alert2: alert2
        });
    },
    /**
   * 获取首页海报
   */
    getIndexPoster() {
        let that = this;
        var leaderinfo = that.data.myleaderinfo;
        //获取团长信息
                common.request({
            endpoint: "share/poster_gd3",
            data: {
                name: leaderinfo.name,
                gname: leaderinfo.other,
                address: leaderinfo.title,
                uid: leaderinfo.leader_id_true,
                lid: leaderinfo.leader_id
            },
            method: "post",
            success(res) {
                console.log(res);
                that.setData({
                    indexPosterData: res
                });
            }
        });
    },
    callService: function(e) {
        var phone = e.currentTarget.dataset.phone;
        if (phone) {
            wx.makePhoneCall({
                phoneNumber: phone
            });
        }
    },
    leaderPass: function() {
        var _this = this;
        let alertMsg = "提示";
        let operation = "操作成功";
        let openc = "是否开启运营";
        wx.showModal({
            titile: alertMsg,
            content: openc,
            success: function(res) {
                if (res.confirm) {
                    common.request({
                        endpoint: "Leader/pretrial/leader-pass",
                        data: {
                            id: _this.data.leader_id
                        },
                        method: "post",
                        success(res) {
                            wx.showToast({
                                icon: "none",
                                title: operation
                            });
                            _this.setData({
                                leader_rest: 1
                            });
                        }
                    });
                }
            }
        });
    },
    pageSwitch: function(e) {
        let that = this;
        wx.showModal({
            title: "提示",
            content: "是否退出团长管理中心",
            success(res) {
                if (res.confirm) {
                    wx.switchTab({
                        url: e.currentTarget.dataset.url
                    });
                }
            }
        });
    },
    scancode: function() {
        var that = this;
        var clerk = that.data.clerk;
        var openid = "";
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        wx.scanCode({
            success: res => {
                console.log(res);
                wx.navigateTo({
                    url: "/" + res.path,
                    success() {},
                    fail(err) {
                        wx.showModal({
                            title: "提示",
                            content: "请扫描正确的提货二维码"
                        });
                    }
                });
            },
            fail(err) {
                wx.showModal({
                    title: "提示",
                    content: "请扫描正确的提货二维码",
                    showCancel: false
                });
            }
        });
    }
});